package FFmpeg

import (
	"bytes"
	"fmt"
	"os"
	"os/exec"
	"strconv"
)
//.下载文件到本地
func DownloadFile(url string,filename string) (err error){
	c := exec.Command("ffmpeg","-i",url,filename)
	if err = c.Run(); err != nil {
		PrintCmdErr(c)
		return
	}
	return
}
//.获取视频音频
func GetAudio(filename string,audioFile string)(err error){
	c := exec.Command("ffmpeg", "-i", filename, "-ss","00:00:00","-b:a","64k","-acodec","mp3","-ar","44100","-ac","1",audioFile)
	if err = c.Run(); err != nil {
		PrintCmdErr(c)
		return
	}
	return
}

//.视频压缩转换MP4
func SwitchMp4(filename string,outputFile string)(err error){
	c := exec.Command("ffmpeg", "-i", filename, "-y","-q","0","-vcodec","libx264",outputFile)
	if err = c.Run(); err != nil {
		PrintCmdErr(c)
		return
	}
	return
}
//.音频截取
func SplitAudio(filename string,startTime string,endTime string,audioFile string)(err error){
	c := exec.Command("ffmpeg", "-i", filename, "-ss","00:00:00","-t",endTime,audioFile)
	if err = c.Run(); err != nil {
		PrintCmdErr(c)
		return
	}
	return
}
//.多音频合并
func MergeAudio(filename string,space float64,nextFilename string,outputFile string)(err error){
	time := strconv.FormatFloat(space*1000,'f', -1, 64)
	c := exec.Command("ffmpeg", "-i",filename, "-i" ,nextFilename ,"-filter_complex", "[1]adelay="+time+"|"+time+"[del1],[0][del1]amix",outputFile)
	if err = c.Run(); err != nil {
		PrintCmdErr(c)
		return
	}
	return
}

//.音视频合并
func MergeMedia(videoFilename string,audioFile string,outputFile string)(err error){
	c := exec.Command("ffmpeg","-i", videoFilename,"-i",audioFile,outputFile)
	if err = c.Run(); err != nil {
		fmt.Println("Error: ", err.Error())
	}
	return
}

func Delete(filenames ...string){
	for _,v := range filenames {
		os.Remove(v)
	}
}

func Commands(){
	//.下载视频到本地处理
	c := exec.Command("ffmpeg","-i","http://mpvideo.qpic.cn/0b78fuaaiaaaaeacwvdeh5pvalodaqwqabaa.f10003.mp4?dis_k=cb67312ebf1c3f4dd2f01b4f76544e0a&dis_t=1600501815","test.mp4")
	if err := c.Run(); err != nil {
		fmt.Println("Error: ", err.Error())
	}
	//.获取视频音频
	c = exec.Command("ffmpeg", "-i", "test.mp4", "-ss","00:00:00","-b:a","64k","-acodec","mp3","-ar","44100","-ac","1","test.mp3")
	if err := c.Run(); err != nil {
		fmt.Println("Error: ", err.Error())
	}
	//.主视频包装TS格式
	c = exec.Command("ffmpeg","-i","test.mp4","-c","copy","-bsf:v","h264_mp4toannexb","-f","mpegts","test.ts")
	if err := c.Run(); err != nil {
		fmt.Println("Error: ", err.Error())
	}
	//.片尾视频包装TS格式
	c = exec.Command("ffmpeg","-i","fuck.mp4","-c","copy","-bsf:v","h264_mp4toannexb","-f","mpegts","fuck.ts")
	if err := c.Run(); err != nil {
		fmt.Println("Error: ", err.Error())
	}
	c = exec.Command("ffmpeg","-i","'concat:test.ts|fuck.ts'","-c","copy","-bsf:a","aac_adtstoasc","-movflags","+faststart","new.mp4")
	if err := c.Run(); err != nil {
		fmt.Println("Error: ", err.Error())
	}
	//.视频压缩，转换格式为mp4
	c = exec.Command("ffmpeg", "-i", "wangzikang.avi", "-y","-q","0","-vcodec","libx264","wangzikang.mp4")
	if err := c.Run(); err != nil {
		fmt.Println("Error: ", err.Error())
	}
	//.视频音频混合
	c = exec.Command("ffmpeg","-i", "wangzikang.mp4","-itsoffset","1.5","-i","test.mp3","wangzikang1.mp4")
	if err := c.Run(); err != nil {
		fmt.Println("Error: ", err.Error())
	}
	//.音频无缝拼接
	c = exec.Command("ffmpeg","-i","'concat:test.mp3|fuck.mp3'","-c","copy", "new2.mp3")
	if err := c.Run(); err != nil {
		fmt.Println("Error: ", err.Error())
	}
	//.音频合并，可设置插入时间
	c = exec.Command("ffmpeg", "-i","test.mp3", "-i" ,"fuck.mp3" ,"-filter_complex","'[1]adelay=6000|6000[del1],[0][del1]amix'","outputx.mp3")
	if err := c.Run(); err != nil {
		fmt.Println("Error: ", err.Error())
	}
}

func PrintCmdErr(c *exec.Cmd){
	var out bytes.Buffer
	var stderr bytes.Buffer
	c.Stdout = &out
	c.Stderr = &stderr
	fmt.Println(out.String())
	fmt.Println(stderr.String())
}
